<?php
/** [类型属性控制器]
 * @Author: 976123967@qq.com
 * @Date:   2015-01-15 13:45:33
 * @Last Modified by:   Administrator
 * @Last Modified time: 2015-01-16 11:22:17
 */
class AttrAction extends PublicAction{
	public $logic;
	public $getTypeid;
	public $postTypeid;
	public function _initialize()
	{
		parent::_initialize();
		$this->logic = D('Attr','Logic');

		$this->getTypeid  = I('get.typeid');
		$this->postTypeid = I('post.typeid');

		if(!$this->getTypeid && !$this->postTypeid )
		{
			$this->error('链接错误');
		}
	}

	/**
	 * [index 属性列表]
	 * @return [type] [description]
	 */
	public function index()
	{
		
		$logic =D('Type','Logic')->get_all();
		$this->assign('typename',$logic[$this->getTypeid]['typename']);

		$data  = $this->logic->get_all_by_typeid($this->getTypeid);
		
		$this->assign('data',$data);
		
		$this->display();
	}


	/**
	 * [add 添加属性]
	 */
	public function add()
	{

		if(IS_POST)
		{
			if(!$this->logic->add_attr())
				$this->error($this->logic->getError());
			$this->success('属性添加成功',U('Attr/index',array('typeid'=>$this->postTypeid)));
			die;
		}

		$logic =D('Type','Logic')->get_all();
		$this->assign('typename',$logic[$this->getTypeid]['typename']);
		
		$this->display();
	}

	/**
	 * [update_cache 更新缓存]
	 * @return [type] [description]
	 */
	public function update_cache()
	{
		$this->logic->update_cache();
		$this->success('属性缓存更新成功',U('Attr/index',array('typeid'=>$this->getTypeid)));

	}

	/**
	 * [edit 编辑]
	 * @return [type] [description]
	 */
	public function edit()
	{

		if(IS_POST)
		{
			if(!$this->logic->update_attr())
				$this->error($this->logic->getError());
			$this->success('属性编辑成功',U('Attr/index',array('typeid'=>$this->postTypeid)));
			die;
		}

		$field = $this->logic->find_one();
		if(!$field)
			$this->error('链接错误');
		$this->assign('field',$field);

		$logic =D('Type','Logic')->get_all();
		$this->assign('typename',$logic[$this->getTypeid]['typename']);
		
		$this->display();
	}

	/**
	 * [del 删除]
	 * @return [type] [description]
	 */
	public function del()
	{
		if(!$this->logic->del_attr())
				$this->error($this->logic->getError());
			$this->success('属性删除成功',U('Attr/index',array('typeid'=>$this->getTypeid)));
	}

	/**
	 * [operation 更新属性操作]
	 * @return [type] [description]
	 */
	public function operation()
	{
		if(!$msg = $this->logic->operation())
			$this->error($this->logic->getError());
		$this->success($msg,U('Attr/index',array('typeid'=>$this->getTypeid)));
	}
}